package com.pactera.madp.daemon.quartz.task;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.constant.SecurityConstants;
import com.pactera.madp.cp.api.dto.track.BehaviorTrackDto;
import com.pactera.madp.cp.api.entity.clientLossManagement.RemindTaskRecordsEntity;
import com.pactera.madp.cp.api.feign.RemoteRemindTaskRecordsService;
import com.pactera.madp.cp.api.feign.RemoteTrackService;
import com.pactera.madp.daemon.quartz.constant.enums.MadpQuartzEnum;
import com.pactera.madp.daemon.quartz.entity.SysJob;
import com.pactera.madp.daemon.quartz.service.SysJobService;
import com.pactera.madp.daemon.quartz.util.TaskUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.pactera.madp.daemon.quartz.constant.enums.MadpQuartzEnum.JOB_STATUS_NOT_RUNNING;

/**
 * @author zkd
 * @date 2022/5/13 11:39
 */
@Slf4j
@Component("noCommunicationRemindTask")
@AllArgsConstructor
public class NoCommunicationRemindTask {

    private RemoteTrackService remoteTrackService;
    private SysJobService sysJobService;
    private final TaskUtil taskUtil;
    private final Scheduler scheduler;
    private RemoteRemindTaskRecordsService remoteRemindTaskRecordsService;



    /**
     * 未交流提醒发送任务
     * @param jsonParams {"wxUserId":"","wxExternalUserId":"","content":""}
     * {
     *   "wxUserId":"成员企业微信id",
     *   "wxExternalUserid":"外部联系人微信id",
     *   "content":"提醒内容"
     * }
     * @return
     */
    @SneakyThrows
    public String sendNoCommunicationRemind(String jsonParams) {
        BehaviorTrackDto dto = JSONUtil.toBean(jsonParams, BehaviorTrackDto.class);
        dto.setEvent("untalk");
        remoteTrackService.handTrackInner(dto, SecurityConstants.FROM_IN);
        log.info("执行未交流提醒任务...,wxUserId:{},wxExternalUserid:{},",dto.getWxUserId(),dto.getWxExternalUserid());
        return MadpQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }

    /**
     * 每天批量创建成员未交流提醒定时任务
     * @return
     */
    @SneakyThrows
    public String noCommunicationRemindTaskBatchCreate() {
        log.info("批量创建成员未交流提醒定时任务开始执行...");
        remoteRemindTaskRecordsService.batchCreateTask(SecurityConstants.FROM_IN);
        return MadpQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }


    /**
     * 每天批量暂停未交流提醒发送任务(包含已达到提醒次数的/不再是未交流的)
     * @return
     */
    @SneakyThrows
    public String pauseSendNoCommunicationRemind() {
        log.info("批量暂停未交流提醒发送任务开始执行...");
        List<Integer> idList = remoteRemindTaskRecordsService.getRecordFinishedIdList(SecurityConstants.FROM_IN);
        //需要被暂停的jobNameList
        ArrayList<String> jobNameList = new ArrayList<>();
        idList.forEach(id->{
            jobNameList.add("sendNoCommunicationRemind"+id);
        });
        List<SysJob> jobs = sysJobService.list(Wrappers.<SysJob>lambdaQuery()
                .eq(SysJob::getJobStatus, "2")
                .eq(SysJob::getJobGroup, "behaviorTrackTask")
                .eq(SysJob::getMethodName, "sendNoCommunicationRemind")
                .in(SysJob::getJobName,jobNameList));

        for (SysJob sysJob : jobs) {
            //更新定时任务状态条件，运行状态2更新为暂停状态3
            this.sysJobService.updateById(SysJob.builder().jobId(sysJob.getJobId())
                    .jobStatus(JOB_STATUS_NOT_RUNNING.getType()).build());
            taskUtil.pauseJob(sysJob, scheduler);
            log.info("成员未交流提醒发送任务已被暂停,job_id:{}",sysJob.getJobId());
        }

        return MadpQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }

}
